﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using RepertorySystem.Models;

namespace RepertorySystem.Controllers
{
    public class StorageListModelsController : ApiController
    {
        private StoragesListDbContext db = new StoragesListDbContext();

        //GET: api/StorageListModels
        public IQueryable<StorageList> GetStorageLists() {
            return db.StorageLists;
        }

        // GET: api/StorageListModels/5
        [ResponseType(typeof(StorageList))]
        public IHttpActionResult GetStorageList(int id) {

            //StorageList storageList = db.StorageLists.Find(id);
            //StorageList storageList = db.StorageLists.SqlQuery("select * from dbo.StorageLists where Book_Id =" + id).First();
            StorageList storageList = db.StorageLists.Where(u => u.Book_Id == id).First();
            if (storageList == null) {
                return NotFound();
            }
            return Ok(storageList);
        }


        // PUT: api/StorageListModels/5
        [ResponseType(typeof(void))]
        public IHttpActionResult PutStorageList(int id, StorageList storageList)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != storageList.ID)
            {
                return BadRequest();
            }

            db.Entry(storageList).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!StorageListExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/StorageListModels
        [ResponseType(typeof(StorageList))]
        public IHttpActionResult PostStorageList(StorageList storageList)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.StorageLists.Add(storageList);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = storageList.ID }, storageList);
        }

        // DELETE: api/StorageListModels/5
        [ResponseType(typeof(StorageList))]
        public IHttpActionResult DeleteStorageList(int id)
        {
            StorageList storageList = db.StorageLists.Find(id);
            if (storageList == null)
            {
                return NotFound();
            }

            db.StorageLists.Remove(storageList);
            db.SaveChanges();

            return Ok(storageList);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool StorageListExists(int id)
        {
            return db.StorageLists.Count(e => e.ID == id) > 0;
        }
    }
}